import { createRouter, createWebHistory, createWebHashHistory } from 'vue-router'
import { useRouter } from 'vue-router'

const router = createRouter({
    history: createWebHistory('pc/'), //路由前缀
    // history: createWebHashHistory('pc/'),
    routes: [
        {
            path: '/',
            name: 'index',
            component: () => import('@/views/index.vue'),
        },
    ],
})
//导航守卫
router.beforeEach((to) => {
    console.log('跳转', to)
    let Authorization = ''
    let loc_ses = localStorage.getItem('loc_ses')
    // console.log('是否保持登录', loc_ses)
    if (loc_ses == 'true') {
        Authorization = localStorage.getItem('Authorization')
    } else {
        Authorization = sessionStorage.getItem('Authorization')
    }
    // if (to.path.startsWith('main') && !Authorization)如果登录后里面所有的页面都是main开头页可以这样写
    if (to.path == '/' && Authorization) {
        return useRouter().replace('/index')
    }
    if (to.path !== '/' && !Authorization) {
        ElMessage({
            message: '登录失效,请重新登录',
            type: 'error',
        })
        return useRouter().replace('/')
    }
})

export default router
